package com.sojson.project.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.controller.param.verify.MeanContentParamVerify;
import com.sojson.project.entity.po.MeanContent;
import com.sojson.project.service.MeanContentService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 协议内容表控制器
 * 
 * @author liu
 * @date 2021-10-26
 */
@Api(tags = "协议内容表管理")
@RestController
@RequestMapping("/meanContent")
public class MeanContentController {

    @Autowired
    private MeanContentService meanContentService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/meanContent/insert')")
    @ApiOperation("给协议内容表添加一条数据")
    public Result<String> insert(@RequestBody MeanContent dto) {
        // 校验参数
        Result<String> paramResult = MeanContentParamVerify.insert(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dto.insertInit();
        meanContentService.insert(dto);
        return Result.success(dto.getId());
    }

    /**
     * 批量插入
     * 
     * @param dto
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/meanContent/insertList')")
    @ApiOperation("给协议内容表添加多条数据")
    public Result<Integer> insertList(@RequestBody MeanContent dto) {
        return Result.success(meanContentService.insertList(dto));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param dto
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/meanContent/deleteByDto')")
    @ApiOperation("通过对象批量删除协议内容表数据(物理删除)")
    public Result<Integer> deleteByDto(@RequestBody MeanContent dto) {
        return Result.success(meanContentService.deleteByDto(dto));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @GetMapping("/countAll")
    @PreAuthorize("@ss.hasPermi('/meanContent/countAll')")
    @ApiOperation("统计协议内容表全部数据(包含逻辑删除的)")
    public Result<Integer> countAll() {
        return Result.success(meanContentService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('/meanContent/listAll')")
    @ApiOperation("查询协议内容表所有数据(包含逻辑删除的)")
    public Result<List<MeanContent>> listAll() {
        return Result.success(meanContentService.listAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param dto
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/meanContent/countByDto')")
    @ApiOperation("根据对象统计协议内容表数据")
    public Result<Integer> countByDto(MeanContent dto) {
        return Result.success(meanContentService.countByDto(dto));
    }

    /**
     * 根据对象查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/listByDto")
    @PreAuthorize("@ss.hasPermi('/meanContent/listByDto')")
    @ApiOperation("根据对象查询协议内容表数据")
    public Result<List<MeanContent>> listByDto(MeanContent dto) {
        return Result.success(meanContentService.listByDto(dto));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/meanContent/deleteById')")
    @ApiOperation("通过id删除协议内容表数据(物理删除)")
    public Result<Integer> deleteById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(meanContentService.deleteById(id));
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/meanContent/deleteByIds')")
    @ApiOperation("通过id数组删除协议内容表数据(物理删除)")
    public Result<Integer> deleteByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(meanContentService.deleteByIds(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateAllById")
    @PreAuthorize("@ss.hasPermi('/meanContent/updateAllById')")
    @ApiOperation("通过id更新协议内容表数据(全部更新)")
    public Result<Integer> updateAllById(@RequestBody MeanContent dto) {
        return Result.success(meanContentService.updateAllById(dto));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateAllByIdList")
    @PreAuthorize("@ss.hasPermi('/meanContent/updateAllByIdList')")
    @ApiOperation("通过id列表更新协议内容表数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByIdList(@RequestBody MeanContent dto) {
        return Result.success(meanContentService.updateAllByIdList(dto));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateDtoListAllById")
    @PreAuthorize("@ss.hasPermi('/meanContent/updateDtoListAllById')")
    @ApiOperation("通过id列表更新协议内容表数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllById(@RequestBody MeanContent dto) {
        return Result.success(meanContentService.updateDtoListAllById(dto));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/meanContent/updateById')")
    @ApiOperation("通过id更新协议内容表数据(局部更新)")
    public Result<Integer> updateById(@RequestBody MeanContent dto) {
        // 校验参数
        Result<Integer> paramResult = MeanContentParamVerify.updatePartialById(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dto.updateInit();
        dto.setUpdateNull();
        return Result.success(meanContentService.updateById(dto));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateByIdList")
    @PreAuthorize("@ss.hasPermi('/meanContent/updateByIdList')")
    @ApiOperation("通过id列表更新协议内容表数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByIdList(@RequestBody MeanContent dto) {
        return Result.success(meanContentService.updateByIdList(dto));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateDtoListById")
    @PreAuthorize("@ss.hasPermi('/meanContent/updateDtoListById')")
    @ApiOperation("通过id列表更新协议内容表数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListById(@RequestBody MeanContent dto) {
        return Result.success(meanContentService.updateDtoListById(dto));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @PreAuthorize("@ss.hasPermi('/meanContent/findById/{id}')")
    @ApiOperation("通过id查询协议内容表数据")
    public Result<MeanContent> findById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(meanContentService.findById(id));
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @GetMapping("/findByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/meanContent/findByIds/{ids}')")
    @ApiOperation("通过id数组查询协议内容表数据")
    public Result<List<MeanContent>> findByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(meanContentService.findByIds(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param dto
     * @return
     */
    @GetMapping("/findIdByDto")
    @PreAuthorize("@ss.hasPermi('/meanContent/findIdByDto')")
    @ApiOperation("通过对象查询协议内容表id列表")
    public Result<List<Integer>> findIdByDto(MeanContent dto) {
        return Result.success(meanContentService.findIdByDto(dto));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/meanContent/clear')")
    @ApiOperation("清空协议内容表数据(物理清空)")
    public Result<Integer> clear() {
        return Result.success(meanContentService.clear());
    }

}